package com.xu.controller;

import com.xu.mapper.ImgMapper;
import com.xu.pojo.Img;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
public class ImgController {

    @Autowired
    private ImgMapper imgMapper;

    @RequestMapping("/toImg")
    public String toImg(Model model)
    {
        List<Img> imgs = imgMapper.queryAllImg();
        model.addAttribute("imgs", imgs);
        return "admin/img/img";
    }

    @RequestMapping("/toAddImg")
    public String toAddImg()
    {
        return "/admin/img/img-add";
    }

//    @RequestMapping("/imgAdd")
//    public String imgAdd(@RequestParam("file") MultipartFile file) {
//        //两件事：加获取路径，
//
//        String path = null;
//        try {
//            if (file != null) {
//                String fileName = file.getOriginalFilename();
//                if (fileName != null && !fileName.isEmpty()) {
//                    File newFile = new File(fileName);
//                    FileOutputStream os = new FileOutputStream(newFile);
//                    os.write(file.getBytes());
//                    os.close();
//                    //把file里的内容复制到奥newFile中
//                    file.transferTo(newFile);
//
//                    String newFileName = file.getName();
//                    String url = "http://localhost:9999/file/" + newFileName;
//                    FileUtils.copyFile(file, new File("/images/" + newFileName));
//
//
//
//                }
//            }
//
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return newFileName;
//    }
    }
